package cn.kpi.service.teamassement.Impl;

import cn.kpi.dao.teamassessment.BUAssTemplateDao;
import cn.kpi.dao.teamassessment.BUKPIDao;
import cn.kpi.entity.teamassessment.BUAssTempCreateIndexEntity;
import cn.kpi.entity.teamassessment.BUAssTemplateEntity;
import cn.kpi.entity.teamassessment.BUAssTemplateIndexEntity;
import cn.kpi.entity.teamassessment.BUAssTemplateOrgEntity;
import cn.kpi.entity.usualassessment.OrgTitle;
import cn.kpi.service.teamassement.IBUAssTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BUAssTemplateServiceImpl implements IBUAssTemplateService {
    @Autowired
    private BUAssTemplateDao buAssTemplateDao;

    @Autowired
    private BUKPIDao bukpiDao;

    public List<BUAssTemplateIndexEntity> GetTemplateIndexListRowSpan() throws Exception {
        try {
            return buAssTemplateDao.GetTemplateIndexListRowSpan();

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public List<BUAssTemplateEntity> GetUsAssTemplateListByWhere(String where) throws Exception {
        try {
            return buAssTemplateDao.GetBuAssTemplateListByWhere(where);

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public int GetUsAssTemplateListCountByWhere(String where) throws Exception {
        try {
            return buAssTemplateDao.GetBuAssTemplateListCountByWhere(where);

        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    @Override
    public List<BUAssTemplateIndexEntity> GetTemplateIndexListByWhere(String where) throws Exception {
        try {
            return buAssTemplateDao.GetTemplateIndexListByWhere(where);

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public BUAssTemplateIndexEntity GetIndexById(Long assindexid) throws Exception {
        try {
            return buAssTemplateDao.GetIndexById(assindexid);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public List<BUAssTempCreateIndexEntity> GetIndexListByWhere(String where) throws Exception {
        try {
            return buAssTemplateDao.GetIndexListByWhere(where);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public int UpdateIndex(BUAssTemplateIndexEntity entity) throws Exception {
        try {
            return buAssTemplateDao.UpdateIndex(entity);
        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    @Transactional
    public int InsertTemplateIndexentity(BUAssTemplateIndexEntity entity) throws Exception {
        try {
            int result = 0;
            if (entity.getParentid() > 0) {
                result += buAssTemplateDao.UpdateParentscore(entity.getParentid(), entity.getScore());
            }
            result += buAssTemplateDao.InsertTemplateIndexentity(entity);
            return result;

        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    @Transactional
    public int DeleteIndex(Long assindexid, Long parentid) throws Exception {
        try {
            int result = 0;
            int score = GetIndexById(assindexid).getScore();
            result += buAssTemplateDao.UpdateParentscore(parentid, (0 - score));
            result += buAssTemplateDao.DeleteIndex(assindexid, parentid);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    @Transactional
    public int DeleteUsTempEntity(Long templateid) throws Exception {
        try {
            int result = buAssTemplateDao.DeleteUsTempCreateIndexEntity(templateid);
            result += buAssTemplateDao.DeleteUsTempEntity(templateid);
            result += buAssTemplateDao.DeleteBuTempOrgBatch(templateid);
            if (result <= 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
                return -1;
            }
            return result;

        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    public BUAssTemplateEntity GetUsAssTemplateListById(Long templateid) throws Exception {
        try {
            return buAssTemplateDao.GetUsAssTemplateListById(templateid);

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public List<BUAssTemplateOrgEntity> GetTempOrgListByTempId(Long templateid) {
        try {
            return buAssTemplateDao.GetTempOrgListByTempId(templateid);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public int InsertBuTempOrgBatch(Long itemids, List<Long> orgids) throws Exception {
        try {
            buAssTemplateDao.DeleteBuTempOrgBatch(itemids);
            int result = buAssTemplateDao.InsertBuTempOrgBatch(itemids, orgids);
            if (result <= 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
                return -1;
            }
            return result;

        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    @Override
    public int DeleteBuTempOrgBatch(Long templateid) {
        try {
            int result = 0;
            int score = buAssTemplateDao.DeleteBuTempOrgBatch(templateid);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    @Override
    public List<BUAssTempCreateIndexEntity> GetTemplateIndexBytemplateid(Long templateid) throws Exception {
        try {
            return buAssTemplateDao.GetTemplateIndexBytemplateid(templateid);

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public List<BUAssTemplateOrgEntity> GetChooseOrgListByTempId(Long templateid) {
        try {
            return buAssTemplateDao.GetChooseOrgListByTempId(templateid);

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public int updateSubmit(Long templateid, int issubmit) {

        try {
            String where = " templateid=" + templateid + " and complete=0 ";

            return buAssTemplateDao.updateSubmit(templateid, issubmit);
//            List<Map> maps = bukpiDao.GetScoreByTemplateid(where);
//            if (maps.size() <= 0)
//                return buAssTemplateDao.updateSubmit(templateid, issubmit);
//            else
//                return buAssTemplateDao.updateSubmit(templateid, 0);
        } catch (Exception ex) {

            return 0;
        }
    }

    @Override
    public List<BUAssTemplateEntity> getTemplateByBeginTime(Date begintime) {
        return buAssTemplateDao.getTemplateByBeginTime(begintime);
    }

    @Override
    public List<BUAssTemplateEntity> getTemplateByBeginTimes(String begintime) {
        return buAssTemplateDao.getTemplateByBeginTimes(begintime);
    }


    @Override
    public int addTitle(OrgTitle orgTitle, String addType) {
        String deleteWhere = "";
        if (!addType.equals("m")) {
            deleteWhere = " section='" + orgTitle.getSection() + "'";

        } else {
            deleteWhere = "templateId=" + orgTitle.getTemplateId() + " and type='" + orgTitle.getType()+"'";

        }
        buAssTemplateDao.deleteTitle(deleteWhere);
        return buAssTemplateDao.addTitle(orgTitle);
    }

    @Override
    public List<Map> getTitle(Long templateId, String type, String str) {

        String[] strings = str.split(",");
        String datatype = strings[2];

        String strWhere = "";
        if (!datatype.equals("m")) {

            strWhere = " section='" +str+ "'";
        }else{
            strWhere="templateId="+templateId+" and type='"+type+"'";
             if(templateId==0)return  new ArrayList<>();


        }
        return buAssTemplateDao.getTitle(strWhere);

    }


    public List<BUAssTemplateIndexEntity> GetTemplateIndexListRowSpanByTempId(Long templateid) throws Exception {
        try {
            return buAssTemplateDao.GetTemplateIndexListRowSpanByTempId(templateid);

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Transactional
    public int CreateTemplateAndIndex(List<BUAssTempCreateIndexEntity> lst, BUAssTemplateEntity entity) throws Exception {
        try {
            int result = buAssTemplateDao.InsertTemplateEntity(entity);
            Long tempid = entity.getTemplateid();
            for (int i = 0; i < lst.size(); i++) {
                lst.get(i).setTemplateid(tempid);
            }
            result += buAssTemplateDao.InsertTempCreateIndexBatch(lst);
            if (result <= 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
                return -1;
            }
            return result;

        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    @Transactional
    public int UpdateUsAssTempEntity(List<BUAssTempCreateIndexEntity> lst, BUAssTemplateEntity entity) throws Exception {
        try {
            int result = buAssTemplateDao.UpdateUsAssTempEntity(entity);
            result += buAssTemplateDao.DeleteUsTempCreateIndexEntity(entity.getTemplateid());
            result += buAssTemplateDao.InsertTempCreateIndexBatch(lst);
            if (result <= 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
                return -1;
            }
            return result;

        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    @Override
    public List<Map> getSumScore(String assindexids) {
        return buAssTemplateDao.getSumScore(assindexids);
    }
    //endregion
}